package org.bendev.ildaplayer;

import org.bendev.ildaplayer.SolrAdapter.Program;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.ImageView;

public class PlayerActivity extends Activity  {
	static final String TAG = "PlayerActivity";

	SegmentPlayer player;
	GestureHandler gh;

	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );

		setContentView( R.layout.activity_player );

		player = new SegmentPlayer( this );
		
		player.setVideoView( (VideoView) findViewById( R.id.videoview ) );
		player.setImageView( (ImageView) findViewById( R.id.imageview ) );
		StatusView sv = (StatusView) findViewById( R.id.statusview );
		sv.setPlayer( player );
		gh = new GestureHandler( this, sv );

		if( getIntent()==null ) return;
		Bundle extras = getIntent().getExtras();
		if( extras==null ) return;
		Program p = (Program) extras.get( ProgramListActivity.PROGRAM );
		if( p==null ) return;
		player.play( p.channel, p.preciseStart );
	}

	@Override
	protected void onPause() {
		super.onPause();
		player.stop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		player.stop();
	}

	@Override
	public boolean onCreateOptionsMenu( Menu menu ) {
		getMenuInflater().inflate( R.menu.main, menu );
		return true;
	}
	
	@Override
	public boolean onKeyDown( int keyCode, KeyEvent event ) {
		Log.i( TAG, "onkeydown" );
		boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK && keyCode != KeyEvent.KEYCODE_VOLUME_UP && keyCode != KeyEvent.KEYCODE_VOLUME_DOWN
				&& keyCode != KeyEvent.KEYCODE_MENU && keyCode != KeyEvent.KEYCODE_CALL && keyCode != KeyEvent.KEYCODE_ENDCALL;
		if( isKeyCodeSupported ){

		}
		return super.onKeyDown( keyCode, event );
	}

	@Override
	public boolean onTouchEvent( MotionEvent event ) {
		gh.onTouchEvent( event );
		return true;
	}
}
